package priv.stones.collections.list;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @Author: Stones
 * @Description:
 * @Date: 2017/11/5 17:27
 */

class Person1{
    private String name;
    private int age;

    public Person1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person1{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Person1 person1 = (Person1) o;

        if (age != person1.age) return false;
        return name.equals(person1.name);
    }

    @Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + age;
        return result;
    }
}
public class HashSet2 {

    public static void main(String[] args) {
        Set<Person1> data = new HashSet<Person1>();
        data.add(new Person1("胡磊", 1));
        data.add(new Person1("胡磊1", 1));
        data.add(new Person1("胡磊2", 1));
        data.add(new Person1("胡磊", 1));
        System.out.println(data);

        Iterator<Person1> iterable = data.iterator();
        while (iterable.hasNext()) {

            System.out.println(iterable.next());
        }

    }

}
